package com.example.yuejian21.nongfusanquan.yuejian21.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian21.nongfusanquan.yuejian21.R;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Activitybean;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Orderactivitybean;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Userbean;
import com.example.yuejian21.nongfusanquan.yuejian21.model.Activitymodel;
import com.example.yuejian21.nongfusanquan.yuejian21.model.Usermodel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OrderactivityActivity  extends AppCompatActivity {
    //声明
    private ImageView left;
    private Button enterAct_button;
    private int u_id;
    private int a_id;
    private TextView a_name,a_price,u_name,u_phone,a_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_act);
        a_id=getIntent().getIntExtra("a_id",-1);

        initViews();//初始化
        initEvents();//点击事件处理
        //getdata();
    }

    private void initViews() {
        a_name = (TextView) findViewById(R.id.name);

        u_name = (TextView) findViewById(R.id.u_name);
        u_phone = (TextView) findViewById(R.id.u_phone);
        a_price = (TextView) findViewById(R.id.price);

        a_address = (TextView) findViewById(R.id.a_address);
        enterAct_button = (Button) findViewById(R.id.enterAct_button);

        left= (ImageView) findViewById(R.id.left);
    }

    private void initEvents() {
        //返回上一页
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        SharedPreferences sp =this.getSharedPreferences("cache", Context.MODE_APPEND);
        u_id = Integer.parseInt(sp.getString("uid","0"));

        Usermodel userModel = new Usermodel();
        Call callu = userModel.getUserInfo(u_id);
        callu.enqueue(new Callback<Userbean>() {
            @Override
            public void onResponse(Call<Userbean> call, Response<Userbean> response) {
                Userbean user = response.body();
                u_name.setText(user.getU_name());
                u_phone.setText(user.getU_phone());
            }
            @Override
            public void onFailure(Call<Userbean> call, Throwable t) {
            }
        });


        Activitymodel activityModel = new Activitymodel();
        Call<Activitybean> call = activityModel.getDetail(a_id);

        call.enqueue(new Callback<Activitybean>() {
            @Override
            public void onResponse(Call<Activitybean> call, Response<Activitybean> response) {
                Activitybean data = (Activitybean) response.body();  //获取数据
                String aname = data.getA_name();
                int aprice = data.getA_price();
                String aaddress=data.getA_address();
                a_name.setText(aname);
                a_price.setText("￥"+ aprice+"");
                a_address.setText(aaddress);


            }

            @Override
            public void onFailure(Call<Activitybean> call, Throwable t) {

            }
        });


        enterAct_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activitymodel activityModel = new Activitymodel();
                Call<Orderactivitybean> call = activityModel.addactivityn(u_id,a_id);
                call.enqueue(new Callback<Orderactivitybean>() {
                    @Override
                    public void onResponse(Call<Orderactivitybean> call, Response<Orderactivitybean> response) {
                        Orderactivitybean enterActBean=response.body();
                        if (Orderactivitybean.class.equals("0")) {
                            Toast.makeText(OrderactivityActivity.this, "失败", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(OrderactivityActivity.this, "报名成功", Toast.LENGTH_LONG).show();
                        }
                    }
                    @Override
                    public void onFailure(Call<Orderactivitybean> call, Throwable t) {

                    }
                });
            }
        });



    }
}